package java_test;

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayDeleteItem {
    public static void main(String[] args) {
        int[] oldarray = new int[]{3, 4, 5, 6, 7};
        int num = 2;
        int[] newArray = new int[oldarray.length - 1];
        for (int i = 0; i < newArray.length; i++) {
            if (num < 0 || num > oldarray.length) {  // 数组越界
                throw new RuntimeException("数组越界");
            }
            if (i < num) {
                newArray[i] = oldarray[i];
            } else {
                newArray[i] = oldarray[i + 1];
            }
        }
        // 打印输出数组内容
        System.out.println(Arrays.toString(oldarray));
        oldarray = newArray;
        System.out.println(Arrays.toString(oldarray));

        ArrayList<String> objArray = new ArrayList<String>();
        objArray.clear();
        objArray.add(0, "第0个元素");
        objArray.add(1, "第1个元素");
        objArray.add(2, "第2个元素");
        System.out.println("数组删除元素前：" + objArray);
        objArray.remove(1);
        System.out.println("数组删除元素第1后：" + objArray);
        objArray.remove("第0个元素");
        System.out.println("数组删除元素后：" + objArray);
    }
}
